package com.kanas.webak.organization.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kanas.framework.business.BusinessHandler;
import com.kanas.framework.business.ResponseMessage;
import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.organization.domain.Calendar;
import com.kanas.webak.organization.domain.Organization;
import com.kanas.webak.organization.service.CalendarService;
import com.kanas.webak.organization.service.OrganizationService;

@Controller
@RequestMapping("app/organization/calendar")
public class CalendarController extends ControllerSupport {

	private CalendarService calendarService;
	private OrganizationService organizationService;

	public OrganizationService getOrganizationService() {
		return organizationService;
	}
	
	@Resource(name="kanas.admin.service.organization")
	public void setOrganizationService(OrganizationService organizationService) {
		this.organizationService = organizationService;
	}	
	
	public CalendarService getCalendarService() {
		return calendarService;
	}

	@Resource(name="com.kanas.webak.organization.service.calendarservice")
	public void setCalendarService(CalendarService calendarService) {
		this.calendarService = calendarService;
	}
	
	@RequestMapping(value="/calendar.html")
	public ModelAndView calendarView(){
		ModelAndView mv=new ModelAndView();
		List<Organization> orgs=organizationService.findAll();
		mv.addObject("currentYear",java.util.Calendar.getInstance().get(java.util.Calendar.YEAR));
		mv.addObject("orgs", orgs);
		mv.setViewName("modules/organization/calendar");
		return mv;
	}
	
	@RequestMapping(value="/{calruleId}",method=RequestMethod.DELETE)
	public @ResponseBody String deleteRule(@PathVariable("calruleId") final String ruleId){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return calendarService.remove(ruleId);
			}
		}, logger);
		return rm.toString();
	}
	
	@RequestMapping(value="/calrule",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> listAll(@RequestParam(value="start",required=false)Integer page,@RequestParam(value="count",required=false)Integer rows,
			@RequestParam(value="oid", required = false)String orgId) {
		Map<String,Object> response = new HashMap<String,Object>();
		PageRequest pageObject = new PageRequest(page, rows);
		response.put("identifier", "id");
		response.put("label", "label");
		if(orgId == null) {
			response.put("items", null);
			response.put("numRows", null);
		} else {
			Page<Calendar> pageReult = calendarService.findByOrg(pageObject,orgId);
			response.put("items", pageReult.getContent());
			response.put("numRows", pageReult.getTotalElements());
		}
		return response;
	}
	
	@RequestMapping(value="/calruleByYear",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> listAllByYear(@RequestParam(value="start",required=false)Integer page,@RequestParam(value="count",required=false)Integer rows,
			@RequestParam(value="oid", required = false)String orgId,@RequestParam(value="year", required = false)String year) {
		Map<String,Object> response = new HashMap<String,Object>();
		PageRequest pageObject = new PageRequest(page, rows);
		response.put("identifier", "id");
		response.put("label", "label");
		if(orgId == null) {
			response.put("items", null);
			response.put("numRows", null);
		} else {
			Page<Calendar> pageReult = calendarService.findByOrgAndYear(pageObject, orgId, year);
			response.put("items", pageReult.getContent());
			response.put("numRows", pageReult.getTotalElements());
		}
		return response;
	}
	
	@RequestMapping(value="/{oid}/calrulenew.html")
	public ModelAndView calendarRuleNewView(@PathVariable("oid")String oId){
		ModelAndView mv=new ModelAndView();
		mv.addObject("orgId", oId);
		mv.setViewName("modules/organization/calendar_rule_new");
		return mv;
	}
	
	@RequestMapping(value="/{crid}/calruleedit.html")
	public ModelAndView calendarRuleEditView (@PathVariable("crid") String cId) {
		ModelAndView mv = new ModelAndView();
		Calendar c = calendarService.findOne(cId);
		mv.addObject("calendar", c);
		mv.setViewName("modules/organization/calendar_rule_edit");
		return mv;
	}
	
	@RequestMapping(value="/{oid}/calschedulenew.html")
	public ModelAndView calendarScheduleNewView(@PathVariable("oid")String oId){
		ModelAndView mv=new ModelAndView();
		mv.addObject("orgId", oId);
		mv.setViewName("modules/organization/calendar_schedule_new");
		return mv;
	}
	
	@RequestMapping("/newrule.do")
	public @ResponseBody ResponseMessage addNew(final Calendar calendar,@RequestParam(value="oid",required=false) final String oid) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return calendarService.addNew(calendar, oid, "rule");
			}
		}, logger);
		return response;
	}
	
	@RequestMapping("/newschedule.do")
	public @ResponseBody ResponseMessage addNewSchedule(final Calendar calendar,@RequestParam(value="oid",required=false) final String oid) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return calendarService.addNew(calendar, oid, "schedule");
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/schedule/{itemId}",method=RequestMethod.DELETE)
	public @ResponseBody String deleteSchedule(@PathVariable("itemId") final String itemId){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return calendarService.removeItem(itemId);
			}
		}, logger);
		return rm.toString();
	}
	
	@RequestMapping("/editrule.do")
	public @ResponseBody ResponseMessage editRule(final Calendar calendar,@RequestParam(value="cid",required=false) final String cid) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return calendarService.edit(calendar,cid);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping("/up.do")
	public @ResponseBody ResponseMessage up(@RequestParam(value="id",required=false) final String cid) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return calendarService.upSortNum(cid);
			}
		}, logger);
		return response;
	}
}
